package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Answer;
import com.han.bom.Exam;
import com.han.bom.ExamResponse;
import com.han.bom.MultipleChoice;
import com.han.bom.Question;
import com.han.bom.Student;
import com.han.dao.AnswerDAO;
import com.han.dao.ExamDAO;
import com.han.dao.ExamResponseDAO;
import com.han.dao.QuestionDAO;

/**
 * Servlet implementation class ViewResultServlet
 */
@WebServlet("/Student/ViewResultServlet")
public class ViewResultServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private Logger logger = Logger.getLogger(getClass().getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewResultServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("Entered ViewResultServlet GET");
		String examID = null;
		String examresponseID = null;
		Student student = null;
		
		HttpSession session = request.getSession();
		
		if(session.getAttribute("student")!=null){
			student = (Student)session.getAttribute("student");
		}else{
			request.setAttribute("msg", "Error!");
			request.setAttribute("type", "fail");
			
			RequestDispatcher rd = request.getRequestDispatcher("Login.jsp");
			rd.forward(request,response);
		}
		
		if(request.getParameter("examID")!=null){
			examID = request.getParameter("examID");
		}
		else{
			request.setAttribute("msg", "Error!");
			request.setAttribute("type", "fail");
			
			RequestDispatcher rd = request.getRequestDispatcher("Student.jsp");
			rd.forward(request,response);
		}
		
		if(request.getParameter("examresponseID")!=null){
			examresponseID = (String)request.getParameter("examresponseID");
		}
		else{
			
		}
		
		
		
		if(student!=null && examID!=null){
		
			ExamResponseDAO examresponsedao = new ExamResponseDAO();
			ExamDAO examdao = new ExamDAO();
			AnswerDAO answerdao = new AnswerDAO();
			QuestionDAO questiondao = new QuestionDAO();
			
			ExamResponse examResponse=null;
			try {
				Exam exam = examdao.retrieveExamById(examID);
				
				ArrayList<Question> questions = questiondao.retrieveQuestionsbyExam(examID);
				examResponse = examresponsedao.retrieveResponseById(examresponseID);
				
				for(Question question:questions){
					ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
					question.setAnswer(choices.get(0));
					question.setChoices(choices);
					
				}
				
				for(Answer answer: examResponse.getAnswers()){
					answer.getQuestion().setQuestion(questiondao.retrieveQuestionById(answer.getQuestion().getQuestionID()).getQuestion());
					
					ArrayList<String> choices = answerdao.retrieveAnswersByQuestion(answer.getQuestion().getQuestionID());
					answer.getQuestion().setAnswer(choices.get(0));
					
					if(answer.getQuestion() instanceof MultipleChoice){
						Collections.sort(choices);
					}
					else{
						Collections.sort(choices);
						Collections.reverse(choices);
					}
					
					answer.getQuestion().setChoices(choices);
				
				}
				
				exam.setQuestions(questions);
				examResponse.setExam(exam);
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
			if(examResponse!=null){
				request.setAttribute("examResponse", examResponse);
				
				RequestDispatcher rd = request.getRequestDispatcher("Exam_View.jsp");
				rd.forward(request,response);
			}

		}
		else{
	
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
	}
}
